package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.constant.CommonConstant;
import com.huaxiafinance.management.dto.request.GoodsReceiptDto;
import com.huaxiafinance.management.dto.request.MallLogisticsDto;
import com.huaxiafinance.management.dto.request.ScoreOrderDto;
import com.huaxiafinance.management.dto.response.LogisticsDto;
import com.huaxiafinance.management.mapper.MallLogisticsMapper;
import com.huaxiafinance.management.mapper.MallOrderMapper;
import com.huaxiafinance.management.mapper.YfqDictionaryMapper;
import com.huaxiafinance.management.model.MallLogistics;
import com.huaxiafinance.management.model.MallOrder;
import com.huaxiafinance.management.model.OrderProduct;
import com.huaxiafinance.management.service.ScoreOrderService;
import com.huaxiafinance.management.util.DateUtil;
import com.huaxiafinance.management.util.MessageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@SuppressWarnings("SpringJavaAutowiringInspection")
@Service
@Transactional
public class ScoreOrderServiceImpl implements ScoreOrderService {

    @Autowired
    private YfqDictionaryMapper yfqDictionaryMapper;

    @Autowired
    private MallOrderMapper mallOrderMapper;

    @Autowired
    private MallLogisticsMapper mallLogisticsMapper;

    /**
     * 查询订单列表
     *
     * @param dto 查询订单请求参数
     * @return 订单列表
     */
    @Override
    public MessageResult queryScoreOrders(ScoreOrderDto dto) throws Exception {
        if (dto.getRows()==null||dto.getPage()==null){
            dto.setPage(CommonConstant.PAGE);
            dto.setRows(CommonConstant.ROWS);
        }else {
            dto.setPage(dto.getPage() * dto.getRows());
        }
        MessageResult messageResult = new MessageResult();
        List<MallOrder> orderList = new ArrayList<MallOrder>();
        int mallOrderSum = 0;
        //计算查询时间
        if (dto.getStartTime()!=null&&dto.getEndTime()!=null
                &&!("").equals(dto.getStartTime())
                &&!("").equals(dto.getEndTime())){
            Map<String, Date> dateMap = DateUtil.getTimeFormat(dto.getStartTime(), dto.getEndTime());
            dto.setBeginTime(dateMap.get("beginTime"));
            dto.setOverTime(dateMap.get("overTime"));
            //查询订单列表
            orderList = mallOrderMapper.queryScoreOrders(dto);
            //计算总数
            mallOrderSum = mallOrderMapper.countScoreOrders(dto);
        }
        messageResult.setData(orderList);
        messageResult.setSuccessResult();
        messageResult.setRows(mallOrderSum);
        return messageResult;
    }

    /**
     * 请求订单详情
     *
     * @param id 请求订单id
     * @return 订单详情
     */
    @Override
    public MessageResult scoreOrdersDetails(String id) throws Exception {
        MessageResult messageResult = new MessageResult();
        Integer orderId = Integer.parseInt(id);
        //查询订单详情
        MallOrder mallOrder = mallOrderMapper.selectById(orderId);
        //查询订单下的所有商品
        List<OrderProduct> orderProductList = mallOrderMapper.queryOrderProduct(orderId);
        //订单下的物流信息
        List<MallLogistics> mallLogisticsList = mallLogisticsMapper.queryMallLogistics(orderId);
        mallOrder.setMallLogisticsList(mallLogisticsList);
        mallOrder.setOrderProductList(orderProductList);
        messageResult.setSuccessResult();
        messageResult.setData(mallOrder);
        return messageResult;
    }

    /**
     * 修改物流信息
     *
     * @param dto 修改物流信息的参数
     * @return 修改成功状态
     */
    @Override
    public MessageResult updateOrder(MallLogisticsDto dto) throws Exception {
        MessageResult messageResult = new MessageResult();
        Integer orderId = dto.getGoodsReceiptDtoList().get(0).getOrderId();
        MallOrder mallOrder = mallOrderMapper.selectByPrimaryKey(orderId);
        //判断是否有订单
        if (mallOrder == null) {
            messageResult.setFailureResult("没有该订单");
            return messageResult;
        }
        //判断订单状态
        switch (mallOrder.getOrderStatus()) {
            case CommonConstant.ORDER_STATUS_THREE:
                if (dto.getGoodsReceiptDtoList().size()>0){
                    mallOrder.setOrderStatus(CommonConstant.ORDER_STATUS_FOUR);
                    mallOrder.setGmtModified(new Date());
                    mallOrderMapper.updateByPrimaryKeySelective(mallOrder);
                }else {
                    messageResult.setFailureResult("物流信息至少一条");
                    return messageResult;
                }
                break;
            case CommonConstant.ORDER_STATUS_FOUR:
                break;
            default:
                messageResult.setFailureResult("该订单状态不允许更新");
                return messageResult;
        }
        //删除已有物流信息
        List<MallLogistics> mallLogisticsList = mallLogisticsMapper.queryMallLogistics(orderId);
        for (MallLogistics mallLogistics:mallLogisticsList){
            mallLogisticsMapper.deleteByPrimaryKey(mallLogistics.getId());
        }
        //商品全部签收，订单状态改为完成
        int n = 0;
        for (GoodsReceiptDto goodsReceiptDto:dto.getGoodsReceiptDtoList()){
            if (goodsReceiptDto.getShippingExtraStatus()!=CommonConstant.EXPRESS_STATUS_TWO){
                n = 1;
            }
        }
        if (n==0){
            mallOrder.setOrderStatus(CommonConstant.ORDER_STATUS_FIVE);
            mallOrder.setGmtModified(new Date());
            mallOrderMapper.updateByPrimaryKeySelective(mallOrder);
        }
        //添加新的物流信息
        for (GoodsReceiptDto goodsReceiptDto:dto.getGoodsReceiptDtoList()){
            MallLogistics mallLogistics = new MallLogistics();
            mallLogistics.setOrderId(orderId);
            mallLogistics.setProName(goodsReceiptDto.getProName());
            mallLogistics.setShippingNumber(goodsReceiptDto.getShippingNumber());
            mallLogistics.setShippingCompany(goodsReceiptDto.getShippingCompany());
            mallLogistics.setShippingCompanyStatus(goodsReceiptDto.getShippingCompanyStatus());
            mallLogistics.setShippingExtraStatus(goodsReceiptDto.getShippingExtraStatus());
            mallLogistics.setShippingStatus(goodsReceiptDto.getShippingStatus());
            mallLogistics.setGmtCreate(new Date());
            mallLogisticsMapper.insertSelective(mallLogistics);
        }
        messageResult.setSuccessResult();
        return messageResult;
    }

    /**
     * 查询快递公司列表以及快递状态列表
     *
     * @return 快递公司列表以及快递状态列表
     */
    @Override
    public MessageResult queryLogisticsList() throws Exception {
        MessageResult messageResult = new MessageResult();
        //查询所有快递公司名称
        List<Map<String, Object>> companyMap = yfqDictionaryMapper.selectDictList(CommonConstant.COURIER_SERVICES_COMPANY);
        //查询所有的快递状态名称
        List<Map<String, Object>> expressStatusMap = yfqDictionaryMapper.selectDictList(CommonConstant.EXPRESS_STATUS);
        LogisticsDto logisticsDto = new LogisticsDto();
        logisticsDto.setCompanyMap(companyMap);
        logisticsDto.setExpressStatusMap(expressStatusMap);
        messageResult.setData(logisticsDto);
        messageResult.setSuccessResult();
        return messageResult;
    }

}
